import { h, type App } from 'vue';
import Form from './index.vue'
import request from '/@/utils/request'

const components = import.meta.glob('./components/**/*.vue',{eager: true})
const componentsMap = {}
Object.keys(components).forEach(key=>{
  const component : any= components[key]
  componentsMap[component?.default?.__name] = component.default
})
export const ComponentsMap = componentsMap
/**
 * 渲染slot
 * @param props 组件属性
 * @param name 属性名称
 * @returns 
 */
export const renderSlot = (props: any,name: string,slotProps: any={}) =>{
  if(typeof props[name] == 'string') {
    return h('span',{},props[name])
  } else if(typeof props[name] == 'object') {
    return props[name]
  } else if(typeof props[name] == 'function') {
    const res = props[name](slotProps)
    if(typeof res === 'string') {
      console.log('uuuuuuu~~~~');
      
      return h('span',{},res)
    }
    return res
  }
}
/**
 * 请求封装
 * @param api 
 * @param params 
 * @returns 
 */
export const requestData = (api:any,params: any): Promise<any> => {
  let apiFun: ((param: any)=>Promise<any>) | undefined
  return new Promise((resolve, reject)=>{
    if(api && typeof api == 'function') {
      apiFun = api
    } else if(api && typeof api == 'string') {
      apiFun = (param: any) => request.post(api as any, param)
    }
    if(apiFun) {
      apiFun({
        ...params
      }).then((res: any)=>{
        resolve(res)
        console.log('wertewrewrxdf');
        
      }).catch(reject)
    } else {
      resolve({})
    }
  })
}
// 安装组件，app.use(Form)
Form.install = (app: App<Element>) =>{
  // 注册组件
  app.component('m-form',Form)
}
export default Form